package com.jkgl.controller;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSON;
import com.jkgl.common.core.domain.R;
import com.jkgl.common.utils.SecurityUtils;
import com.jkgl.entity.UserInfoSelfEntity;
import com.jkgl.model.userinfoself.UserInfoSelfPagination;
import com.jkgl.model.userinfoself.UserInfoSelfDTO;
import com.jkgl.service.UserInfoSelfService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author w020304m
 * @since 2025-02-11
 */
@RestController
@RequestMapping("gather/Self")
@Slf4j
@RequiredArgsConstructor
public class UserInfoSelfController {
    private final UserInfoSelfService userInfoSelfService;


//    @GetMapping("/list")
//    @ApiOperation("查询所有用户资料信息")
//    public R list(UserInfoSelfPagination userInfoSelfPagination){
//        List<UserInfoSelfEntity> list=userInfoSelfService.getList(userInfoSelfPagination);
//        return R.ok(list);
//    }

    @GetMapping
    @ApiOperation("获取当前用户自身信息")
    public R userInfo(){
        Long userId = SecurityUtils.getUserId();
        UserInfoSelfEntity infoSelf=userInfoSelfService.getInfoSelf(userId);
        return R.ok(infoSelf);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    public R info(@PathVariable("id") String id){
        UserInfoSelfEntity entity = userInfoSelfService.getById(id);
        if(entity == null){
            return R.fail("获取信息失败");
        }
        return R.ok(entity);
    }

    @PostMapping
    @ApiOperation("创建/更新")
    @Transactional
    public R create(@RequestBody UserInfoSelfDTO userInfoSelfDTO){
        UserInfoSelfEntity infoSelf = BeanUtil.toBean(userInfoSelfDTO,UserInfoSelfEntity.class);
        infoSelf.setUserId(SecurityUtils.getUserId());
        userInfoSelfService.create(infoSelf);
        return R.ok("保存成功");
    }


}
